<?php 
include('./fun.php');include('./hd_fun.php');
$conn=my_sql($cfg);
$re=adm_login();
if($re['err']=='no_login'){
$re=user_login();
if($re['err']=='no_login'){direct('login.php?t='.time());exit;}
if($re['err']!='ok'){die($re['err']);}
$user=$re['user'];
$shop_id=$re['shop_id'];
$shop=get_shop();
}else{
if($re['err']!='ok'){die($re['err']);}
$adm=$re['adm'];
}

$set=get_set();

$ac=g('ac');
$action=g('action');
$imgs=g('img');
if($imgs){
$im=explode('||',$imgs);
foreach($im as $v){
if($v){
$x=explode('|',$v);
$img_temp[$x[0]]++;
}
}
}

$max=g('max');
if(!$max or !is_numeric($max)){$max=1;}

if(!$action){$action='select_this_img($(this));';}
if($max>1){
$select_ac=$action;
$action='choise_img($(this))';}

$sql='select * from '.$cfg['e'].'img where  state=1 ';

if($adm){
$sql.=' and adm_id="'.$adm['id'].'" ';
}else{
$sql.=' and shop_id="'.$shop['id'].'" and c="pro" ';
}

$c=g('c');
if($c){$sql.=' and c="'.a($c).'" ';}else{
}

$page=g('page');if(!$page){$page=1;}
$page_num=100;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;

$conn=my_sql($cfg);
$l=db($sql);
if($l){
foreach($l as $v){
if($v['url']){
$img=$v['url'];
}else{
$img=$v['id'].'|'.$v['ext'];
}

$im=get_img($img,'_240');
$im480=get_img($img,'_480');
$im750=get_img($img,'_750');
$im1024=get_img($img,'_1080');

//echo $img;
$h.='<img_item class="m img_item_'.$v['id'].'" d="'.$v['id'].'" ext="'.$v['ext'].'" x="'.$img.'" onclick="'.$action.'" img_480="'.$im480.'" img_750="'.$im750.'" img_1080="'.$im1024.'" src="'.$im.'"><item style="background:url('.$im.') no-repeat center;">
<num></num>
'.(($img_temp[$v['id']] or $img_temp[$v['url']])?'<x_select></x_select>':'').'

</item>';

$h.='</img_item>';
}}

if($ac=='reload'){
if(!$h){$h='no_img';}
echo $h;exit;}

?>
<style>
img_list{float:left;width:100%;height:100%;overflow:hidden;overflow-y:auto;padding-top:50px;}
img_bt{position:absolute;width:100%;top:0;z-index:100;height:40px;}
img_bt a{color:#fff;background:#006eff;margin-top:5px;}
img_list img_item{float:left;width:12.5%;padding:5px;height:100px;}
img_list item{float:left;width:100%;height:100%;}
img_list span{position:absolute;width:100%;height:30px;text-align:center;line-height:30px;bottom:0;left:0;background:rgba(0,0,0,0.5);color:#fff;font-size:12px;overflow:hidden;}
img_list item img{float:left;width:100% !important;height:100%;}
img_item num{display:none;color:#fff;position:absolute;top:5px;right:5px;width:20px;height:20px;background:green;border-radius:50%;line-height:20px;text-align:center;}
img_list .is_select num{display:block;}
#img_is_select{display:none;}
x_select{position:absolute;width:100%;height:100%;background:rgba(0,0,0,0.5);left:0;top:0}

</style>
<img_bt>
<index>
<a class="m " onclick="$('#up_img').click()">上传图片</a>
<?php if($max>0){echo '<a class="m b" onclick="'.$select_ac.'" id="img_is_select">确定选择</a>';}?>
</index>
</img_bt>
<img_list ac="<?php echo $action; ?>">

<?php if($h){echo $h;}else{no_data();}?>

</img_list>

<ac style="display:none;"></ac>
<script>
window.page=1;
window.img_web='<?php echo $cfg['img_web'];?>';
window.up_finish_ac='load_imgs();';
window.can_load=1;
window.is_select_img={};
$(document).ready(function(){

$('img_list').scroll(function(){
var top=$(this).scrollTop();
var height=$(this).find('main').height();
var h=$(this).height();

if((height-top-h)<300){
 if(window.can_load==1){
 window.page++;
 load_imgs(window.page);
 } 
}
});
});

function load_imgs(page,o){if(!page){page=1;}var c='';
if(o){c=o.html();}window.can_load=0;
$('ac').load('select_img.php?ac=reload&page='+page+'&c='+c+'&action=<?php echo $action;?>',function(d){
    if(d=='no_img'){return;}
if(page==1){$('img_list').html(d);}else{
$('img_list').append(d);
}
window.can_load=1;
});
}

function choise_img(o){
var id=o.attr('d');var max=parseInt('<?php echo $max?>');
var obj=$('.img_item_'+id);
if(!window.is_select_img){window.is_select_img={}}
var total=Object.keys(window.is_select_img).length;
if(obj.hasClass('is_select')){obj.removeClass('is_select');
delete(window.is_select_img['img_item_'+id]);
}else{
if(total>=max){msg('您只能选择'+max+'张图片');return;}
obj.addClass('is_select');window.is_select_img['img_item_'+id]=id;}
img_num_format();
}
function img_num_format(){

if(!window.is_select_img){window.is_select_img={}}
var num=0;
for(var i in window.is_select_img){
num++;
$('.'+i+' num').html(num);
}
if(num>0){$('#img_is_select').fadeIn(300);}else{$('#img_is_select').fadeOut(300);}

}

</script>